/*
 * AnimationReader.cpp
 *
 *  Created on: 2009/02/06
 *      Author: Yongshik Cho
 */

#include "AnimationReader.h"
#include "../core/Rect2D.h"

const std::string AnimationReader::_NAME_TAG			= "name";
const std::string AnimationReader::_TIME_TAG			= "time";
const std::string AnimationReader::_FRAME_TAG			= "Frame";
const std::string AnimationReader::_P1_X_TAG			= "p1x";
const std::string AnimationReader::_P1_Y_TAG			= "p1y";
const std::string AnimationReader::_P2_X_TAG			= "p2x";
const std::string AnimationReader::_P2_Y_TAG			= "p2y";
const std::string AnimationReader::_P3_X_TAG			= "p3x";
const std::string AnimationReader::_P3_Y_TAG			= "p3y";
const std::string AnimationReader::_P4_X_TAG			= "p4x";
const std::string AnimationReader::_P4_Y_TAG			= "p4y";
const std::string AnimationReader::_X_TAG				= "x";
const std::string AnimationReader::_Y_TAG				= "y";
const std::string AnimationReader::_WIDTH_TAG			= "width";
const std::string AnimationReader::_HEIGHT_TAG			= "height";
const std::string AnimationReader::_LOOPTYPE_TAG		= "LoopType";
const std::string AnimationReader::_LOOPCOUNT_TAG		= "LoopCount";
const std::string AnimationReader::_LOOPCOUNT_VALUE		= "LOOP_COUNT";
const std::string AnimationReader::_ENDLESS_VALUE		= "ENDLESS";


void AnimationReader::parse(TiXmlElement* element, Tile* tile)
{
	if(!element)
		return;

	char* name = (char*)element->Attribute(_NAME_TAG.c_str());
	TiXmlElement* node = element->FirstChildElement(_NAME_TAG.c_str()); if(node) name = (char*)node->GetText();

	if(!name)
		return;

	Animatable* animatable = new Animatable();
	if(!animatable)
		AltoThrow(Exception::ERR_MEMORY);

	char* loopType = (char*)element->Attribute(_LOOPTYPE_TAG.c_str());
	node = element->FirstChildElement(_LOOPTYPE_TAG.c_str()); if(node) loopType = (char*)node->GetText();
	if(loopType)
	{
		if(_LOOPCOUNT_VALUE == loopType)
			animatable->setLoopType(Animatable::LOOP_COUNT);
		else if(_ENDLESS_VALUE == loopType)
			animatable->setLoopType(Animatable::ENDLESS);
	}
	else animatable->setLoopType(Animatable::LOOP_COUNT);

	int32 loopCount = 0;
	element->Attribute(_LOOPCOUNT_TAG.c_str(), &loopCount);
	node = element->FirstChildElement(_LOOPCOUNT_TAG.c_str()); if(node) loopCount = atoi(node->GetText());

	animatable->setLoopCount(loopCount);

	for(TiXmlElement* frame=element->FirstChildElement(_FRAME_TAG.c_str());frame;frame=frame->NextSiblingElement(_FRAME_TAG.c_str()))
	{
		int32 time = 0, p1x = 0, p1y = 0, p2x = 0, p2y = 0, p3x = 0, p3y = 0, p4x = 0, p4y = 0, x = 0, y = 0, width = 0, height = 0;
		bool isWidthHeight, attributeType;
		if(frame->Attribute(_TIME_TAG.c_str(), &time))
			attributeType = true;
		else
			attributeType = false;

		if(attributeType)
		{
			if(frame->Attribute(_WIDTH_TAG.c_str(), &width))
			{
				frame->Attribute(_X_TAG.c_str(), &x);
				frame->Attribute(_Y_TAG.c_str(), &y);
				frame->Attribute(_HEIGHT_TAG.c_str(), &height);

				isWidthHeight = true;
			}
			else
			{
				frame->Attribute(_P1_X_TAG.c_str(), &p1x);
				frame->Attribute(_P1_Y_TAG.c_str(), &p1y);
				frame->Attribute(_P2_X_TAG.c_str(), &p2x);
				frame->Attribute(_P2_Y_TAG.c_str(), &p2y);
				frame->Attribute(_P3_X_TAG.c_str(), &p3x);
				frame->Attribute(_P3_Y_TAG.c_str(), &p3y);
				frame->Attribute(_P4_X_TAG.c_str(), &p4x);
				frame->Attribute(_P4_Y_TAG.c_str(), &p4y);

				isWidthHeight = false;
			}
		}
		else
		{
			node = element->FirstChildElement(_TIME_TAG.c_str()); if(node) time = atoi(node->GetText());
			node = element->FirstChildElement(_WIDTH_TAG.c_str()); if(node) width = atoi(node->GetText());
			if(node)
			{
				node = element->FirstChildElement(_X_TAG.c_str()); x = atoi(node->GetText());
				node = element->FirstChildElement(_Y_TAG.c_str()); y = atoi(node->GetText());
				node = element->FirstChildElement(_HEIGHT_TAG.c_str()); height = atoi(node->GetText());

				isWidthHeight = true;
			}
			else
			{
				node = element->FirstChildElement(_P1_X_TAG.c_str()); if(node) p1x = atoi(node->GetText());
				node = element->FirstChildElement(_P1_Y_TAG.c_str()); if(node) p1y = atoi(node->GetText());
				node = element->FirstChildElement(_P2_X_TAG.c_str()); if(node) p2x = atoi(node->GetText());
				node = element->FirstChildElement(_P2_Y_TAG.c_str()); if(node) p2y = atoi(node->GetText());
				node = element->FirstChildElement(_P3_X_TAG.c_str()); if(node) p3x = atoi(node->GetText());
				node = element->FirstChildElement(_P3_Y_TAG.c_str()); if(node) p3y = atoi(node->GetText());
				node = element->FirstChildElement(_P4_X_TAG.c_str()); if(node) p4x = atoi(node->GetText());
				node = element->FirstChildElement(_P4_Y_TAG.c_str()); if(node) p4y = atoi(node->GetText());

				isWidthHeight = false;
			}
		}

		if(isWidthHeight)
		{
			animatable->addFrame(
					AnimationFrame(time, Rect2D(Vector2D((float)x, (float)y), Vector2D((float)width, (float)height)))
					);
		}
		else
		{
			animatable->addFrame(
					AnimationFrame(time, Rect2D(Vector2D((float)p1x, (float)p1y), Vector2D((float)p2x, (float)p2y),
							Vector2D((float)p3x, (float)p3y), Vector2D((float)p4x, (float)p4y)))
					);
		}
	}

#ifdef UNICODE
	{
		TCHAR wstr[512]; memset(wstr, 0, sizeof(wstr));
		::MultiByteToWideChar(CP_UTF8, 0, name, (int32)strlen(name), wstr, 512);
		tile->setAnimatable(wstr, animatable);
	}
#else
	tile->setAnimatable(name, animatable);
#endif
}
